package com.stylefeng.guns.rest.modular.wz.api.v3.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.rest.common.constant.Consts;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.common.util.AliyunSmsUtil;
import com.stylefeng.guns.rest.domain.ParamMap;
import com.stylefeng.guns.rest.domain.Res;
import com.stylefeng.guns.rest.enums.EnWz;
import com.stylefeng.guns.rest.enums.EnWzMapActivity;
import com.stylefeng.guns.rest.enums.EnWzPrize;
import com.stylefeng.guns.rest.modular.auth.util.JwtTokenUtil;
import com.stylefeng.guns.rest.modular.wz.api.v3.service.IWzMapActivityService;
import com.stylefeng.guns.rest.modular.wz.cache.CacheService;
import com.stylefeng.guns.rest.modular.wz.controller.WzFinanceService;
import com.stylefeng.guns.rest.modular.wz.service.IPfFileService;
import com.stylefeng.guns.rest.modular.wz.service.IWzCouponService;
import com.stylefeng.guns.rest.modular.wz.util.AliOssUtil;
import com.stylefeng.guns.rest.modular.wz.util.AliPushUtil;
import com.stylefeng.guns.rest.modular.wz.util.wz.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author ruiyifeng123
 * @since 2018-12-10
 */
@Service
public class MapActivityServiceImpl extends ServiceImpl<WzMapActivityMapper, WzMapActivity> implements IWzMapActivityService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private IWzMapActivityService mapActivityService;
    @Autowired
    private WzAdvMapMapper advMapMapper;
    @Autowired
    private WzShareMapper shareMapper;
    @Autowired
    private WzAdvShareMapper advShareMapper;
    @Autowired
    private WzUserActivityMapper userActivityMapper;
    @Autowired
    private WzCouponMapper couponMapper;
    @Autowired
    private WzMapActivityMapper mapActivityMapper;
    @Autowired
    private IPfFileService fileService;
    @Autowired
    private IWzCouponService couponService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WzUserCouponMapper userCouponMapper;
    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private WzFinanceService financeService;
    @Autowired
    private WzCouponOrderMapper couponOrderMapper;
    @Autowired
    private WzSysMapper sysMapper;
    @Autowired
    private WzAdvMapStaffMapper advMapStaffMapper;



    @Transactional
    public Res deleteActivity(ParamMap paramMap) throws Exception {
        Long mapActivityId = paramMap.getLongMisValid("mapActivityId");
        WzMapActivity mapActivity = cacheService.mapActivity(mapActivityId);
        if (mapActivity.getStatus() != EnWzMapActivity.STATUS_REJECT.getIndex()) {
            return Res.fail("只能删除驳回活动！");
        }
        Map coupon = cacheService.couponData(mapActivity.getCouponId());
        //活动新增券
        if ("2".equals(coupon.get("type").toString())) {
            //兑换券删除图片
            if (mapActivity.getSubtype() == EnWzMapActivity.SUBTYPE_DHQ.getIndex()) {
                String pic = mapActivity.getPic();
                String file_url = pic.substring(pic.length() - 40, pic.length());
                PfFile file = fileService.selectOne(new EntityWrapper<PfFile>().eq("file_url", file_url));
                AliOssUtil.deleteObject(file.getFileUrl(), file.getType(), file.getBucketName());
                fileService.deleteById(file.getFileId());
            }
            boolean a = couponService.deleteById(mapActivity.getCouponId());
            if (a) {
                a = mapActivityService.deleteById(mapActivityId);
                if (a) {
                    return Res.success();
                }
            }
        } else {
            boolean a = mapActivityService.deleteById(mapActivityId);
            if (a) {
                return Res.success();
            }
        }
        return Res.fail();
    }

    public Res list(ParamMap paramMap, Wrapper e) {
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        String mapActivityListSql = "map_activity_id As mapActivityId,adv_map_id As advMapId,name,logo,subtype,join_number As joinNumber,need_share_number As needShareNumber,benefit,begin_time As beginTime,end_time As endTime,pic,usetype,jointype,total_number As totalNumber";
        List<Map<String, Object>> mapActivityList = mapActivityMapper.selectMapsPage(new Page<>(pageNum, pageSize), e.setSqlSelect(mapActivityListSql));//活动列表
        for(Map<String, Object> map:mapActivityList){
            WzAdvMap advMap = cacheService.advMap(Long.parseLong(map.get("advMapId").toString()));
            map.put("longitude", advMap.getLongitude());
            map.put("latitude", advMap.getLatitude());
        }
        //5天内我参加的活动和一人只能参加一次的活动
        Date date = DateUtil.getDateBefore(new Date(), 5);
        String userActivityListSql = "user_map_activity_id As userMapActivityId,map_activity_id As mapActivityId,status,create_time As createTime,share_number As shareNumber,end_time As endTime";
        //每人每天可领一次
        List<Map<String, Object>> userActivityList = userActivityMapper.selectMaps(new EntityWrapper<WzUserActivity>().setSqlSelect(userActivityListSql).eq("user_code", userCode).eq("usetype", EnWzMapActivity.TYPE_DAILY.getIndex()).ge("create_time", date));
        //每人只可领取一次
        List<Map<String, Object>> onlyOnceActivityList = userActivityMapper.selectMaps(new EntityWrapper<WzUserActivity>().setSqlSelect(userActivityListSql).eq("user_code", userCode).eq("usetype", EnWzMapActivity.TYPE_ONLYONCE.getIndex()).ge("end_time", new Date()));
        for (Map<String, Object> map : onlyOnceActivityList) {
            userActivityList.add(map);
        }
        paramMap.put("mapActivityList", mapActivityList);
        paramMap.put("userActivityList", userActivityList);
        return Res.success(paramMap);
    }

    public Res scanJoinActivity(ParamMap paramMap) throws Exception{
        Long mapActivityId = paramMap.getLongMisValid("mapActivityId");
        Long userCode = paramMap.getLongMisValid("userCode");
        Long releaseUserCode = jwtTokenUtil.getUserCodeFromToken();
        WzMapActivity mapActivity = cacheService.mapActivity(mapActivityId);
        Long advMapId=mapActivity.getAdvMapId();
        List<Object> userCodeList=advMapStaffMapper.selectObjs(new EntityWrapper<WzAdvMapStaff>().setSqlSelect("user_code AS userCode").eq("adv_map_id",advMapId));
        userCodeList.add(mapActivity.getUserCode());
        boolean f=userCodeList.contains(releaseUserCode);
        if(!f){
            return Res.fail("此用户不可以扫码!");

        }
        if(mapActivity.getJointype() == EnWzMapActivity.JOINTYPE_Scan_Join.getIndex()){
            //参与活动
            Long r = addUserActivity(mapActivityId, userCode);
            if(r > 6){
                List<WzUser> users = userMapper.selectList(new EntityWrapper<WzUser>().eq("user_code",userCode));
                JSONObject j = new JSONObject();
                j.put("code","100");
                j.put("mapActivityId",mapActivityId);
                j.put("type",3);
                j.put("userMapActivityId",r);
                j.put("msg","扫码成功");
                if(users.get(0).getMobileType() == 1){//ios
                    AliPushUtil.pushMessageToIOS("参与活动扫码成功",userCode.toString(),j);
                }else if(users.get(0).getMobileType() == 2){
                    AliPushUtil.pushMessageToAndroid("参与活动扫码成功",userCode.toString(),j);
                }
                return Res.success("参与成功！");
            }else{
                return Res.fail("此活动已参与！");
            }
        }else{
            return Res.fail("此活动是扫码参与活动！");
        }
    }


    public Res myList(ParamMap paramMap) {
        int pageNum = paramMap.getIntMisValid("pageNum");
        int pageSize = paramMap.getIntMisValid("pageSize");
        int status = paramMap.getIntMisValid("status");//1:进行中;2:已完成
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        EntityWrapper<WzUserActivity> e = new EntityWrapper<WzUserActivity>();
        e.eq("user_code", userCode);
        if(status == 1){
            e.in("status",new Integer[]{EnWzMapActivity.USER_ACTIVITY_Going.getIndex(),EnWzMapActivity.USER_ACTIVITY_End.getIndex()});
        }else if(status == 2){
            e.eq("status", EnWzMapActivity.USER_ACTIVITY_Expired.getIndex());
        }
        List<Map<String, Object>> list = userActivityMapper.selectMapsPage(new Page<>(pageNum, pageSize),e);
        for (Map<String, Object> map : list) {
            WzMapActivity mapActivity = cacheService.mapActivity(Long.parseLong(map.get("mapActivityId").toString()));
            map.put("name", mapActivity.getName());
            map.put("advMapId", mapActivity.getAdvMapId());
            map.put("logo", mapActivity.getLogo());
            map.put("benefit", mapActivity.getBenefit());
            map.put("pic", mapActivity.getPic());
            map.put("subtype", mapActivity.getSubtype());
            map.put("needShareNumber", mapActivity.getNeedShareNumber());
            WzAdvMap advMap = cacheService.advMap(Long.parseLong(map.get("advMapId").toString()));
            map.put("longitude", advMap.getLongitude());
            map.put("latitude", advMap.getLatitude());
        }
        int ongoingNumber = userActivityMapper.selectCount(new EntityWrapper<WzUserActivity>().eq("user_code", userCode).in("status", new Integer[]{EnWzMapActivity.USER_ACTIVITY_Going.getIndex(),EnWzMapActivity.USER_ACTIVITY_End.getIndex()}));
        int finishNumber = userActivityMapper.selectCount(new EntityWrapper<WzUserActivity>().eq("user_code", userCode).eq("status",EnWzMapActivity.USER_ACTIVITY_Expired.getIndex()));
        paramMap.put("ongoingNumber", ongoingNumber);
        paramMap.put("finishNumber", finishNumber);
        paramMap.put("list", list);
        return Res.success(paramMap);
    }

    public Res detail(ParamMap paramMap) {
        Long mapActivityId = paramMap.getLongMisValid("mapActivityId");
        Long userMapActivityId = paramMap.getLongValid("userMapActivityId");
        Integer isParticipate=paramMap.getInt("isParticipate");//是否参与活动 1：参与
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        //基本信息
        Map<String, Object> basicInfo = new HashMap<>();
        //有效点击好友
        List<Map<String, Object>> shareList = new ArrayList<>();
        //活动详情
        WzMapActivity mapActivity = cacheService.mapActivity(mapActivityId);
        basicInfo.put("needShareNumber", mapActivity.getNeedShareNumber());//需要分享人数
        basicInfo.put("activityBeginTime",mapActivity.getBeginTime());
        basicInfo.put("activityEndTime",mapActivity.getEndTime());
        //地图活动对应的地图广告
        WzAdvMap advMap = cacheService.advMap(mapActivity.getAdvMapId());
        basicInfo.put("title", "帮我点一下，我正在参与"+advMap.getName()+"的活动");
        basicInfo.put("desc", advMap.getSubtitle());
        basicInfo.put("logo", advMap.getLogo());
        //查看分享内容链接
        basicInfo.put("shareLink", advMap.getShareLink());
        String rewardShareLink = "";
        if (Consts.dev.equals("true")) {
            rewardShareLink = "http://50wz.viphk1.ngrok.org/wz/wzShare/mapActivity?advMapId=" + mapActivity.getAdvMapId() + "&type=1&mapActivityId=" + mapActivityId + "&userCode=" + userCode+"&adCode="+advMap.getAdCode();
        } else {
            Map<String,Object> commomSys = cacheService.commomSys("commomSys");
            if(commomSys.get("ios_version").toString().equals("1.5.0")){
                rewardShareLink = Consts.Adv_Share_Api_Domain + "/wz/wzShare/mapActivity?advMapId=" + mapActivity.getAdvMapId() + "&type=1&mapActivityId=" + mapActivityId + "&userCode=" + userCode+"&adCode="+advMap.getAdCode();
            }else{
                rewardShareLink = "http://50wz.viphk1.ngrok.org/wz/wzShare/mapActivity?advMapId=" + mapActivity.getAdvMapId() + "&type=1&mapActivityId=" + mapActivityId + "&userCode=" + userCode+"&adCode="+advMap.getAdCode();
            }
        }
        //分享出去的链接
        basicInfo.put("rewardShareLink", rewardShareLink);
        basicInfo.put("userMapActivityId",userMapActivityId);

        // 礼券详情
        Map<String, Object> coupon = cacheService.couponData(mapActivity.getCouponId());
        WzAdvMap couponAdvMap= cacheService.advMap(Long.parseLong(coupon.get("advMapId").toString()));
        coupon.put("name", couponAdvMap.getName());
        coupon.put("latitude", couponAdvMap.getLatitude());
        coupon.put("longitude", couponAdvMap.getLongitude());
        if (userMapActivityId != null) {//参与了活动
            WzUserActivity userActivity = userActivityMapper.selectById(userMapActivityId);
            basicInfo.put("shareNumber", userActivity.getShareNumber());//有效分享人数
            basicInfo.put("endTime", userActivity.getEndTime());//参与活动结束时间
            String effectivecClickListSql = "avatar_url As avatarUrl,nick_name As nickName,create_time As createTime";
            shareList = shareMapper.selectMaps(new EntityWrapper<WzShare>().setSqlSelect(effectivecClickListSql).eq("type", EnWz.SHARE_Type_Activity.getIndex()).eq("target_id", userMapActivityId).eq("user_code", userCode).orderBy("create_time", false));
            String sql = "user_coupon_id As userCouponId,pic,adv_map_id As advMapId,subtype,type,status,benefit,rule,begin_time As beginTime,end_time As endTime";
            List<Map<String, Object>> userCoupons = userCouponMapper.selectMaps(new EntityWrapper<WzUserCoupon>().setSqlSelect(sql).eq("user_code", userCode).eq("user_map_activity_id", userMapActivityId));
            if (userCoupons.size() > 0) {
                //地图活动对应的地图广告
                couponAdvMap = cacheService.advMap(Long.parseLong(userCoupons.get(0).get("advMapId").toString()));
                userCoupons.get(0).put("name",couponAdvMap.getName());
                coupon = userCoupons.get(0);
            }
        } else {//没参与活动
            basicInfo.put("shareNumber", 0);//有效分享
            basicInfo.put("endTime", "");//参与活动结束时间
            if(mapActivity.getJointype() == EnWzMapActivity.JOINTYPE_Direct_Join.getIndex() && isParticipate!=null && isParticipate==1){
                //参与活动
                userMapActivityId = addUserActivity(mapActivityId, userCode);
                if(userMapActivityId > 6){
                    WzUserActivity userActivity = userActivityMapper.selectById(userMapActivityId);
                    basicInfo.put("endTime", userActivity.getEndTime());//活动结束时间
                    basicInfo.put("userMapActivityId",userMapActivityId);
                }
            }
        }
        //数据收集
        paramMap.clear();
        paramMap.put("basicInfo", basicInfo);
        paramMap.put("shareList", shareList);
        paramMap.put("coupon", coupon);
        return Res.success(paramMap);
    }

    public Res edit(ParamMap paramMap) throws Exception{
        Long mapActivityId = paramMap.getLongMisValid("mapActivityId");
        WzMapActivity mapActivity = cacheService.mapActivity(mapActivityId);
        WzAdvMap advMap = cacheService.advMap(mapActivity.getAdvMapId());
        WzCoupon coupon = couponMapper.selectById(mapActivity.getCouponId());
        int needShareNumber = paramMap.getIntMisValid("needShareNumber");//需要分享人数
        int usetype = paramMap.getIntMisValid("usetype");//活动限制:1-限每人每天可领一次；2-限每人只可领一次；
        int totalNumber = paramMap.getIntMisValid("totalNumber");//活动总数量
        int jointype = paramMap.getIntMisValid("jointype");//活动类型：1-直接参与；2-扫码参与；
        String beginTimeZ = paramMap.getStrMisValid("activityBeginTime");//活动开始时间
        Date activityBeginTime = DateUtil.toDateTime(beginTimeZ + " 00:00:00");
        String endTimeZ = paramMap.getStrMisValid("activityEndTime");//活动结束时间
        Date activityEndTime = DateUtil.toDateTime(endTimeZ + " 23:59:59");
        mapActivity.setNeedShareNumber(needShareNumber);
        mapActivity.setUsetype(usetype);
        mapActivity.setTotalNumber(totalNumber);
        mapActivity.setJointype(jointype);
        mapActivity.setBeginTime(activityBeginTime);
        mapActivity.setEndTime(activityEndTime);
        mapActivity.setStatus(EnWzMapActivity.STATUS_CHECKING.getIndex());
        int amount = paramMap.getIntMisValid("amount");//费用
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        WzUser user = cacheService.user(userCode);//发布用户
        //活动新增券
        if (coupon.getType() == EnWzPrize.TYPE_Activity_Coupon.getIndex()) {
            String rule = paramMap.getStrMisValid("rule");//规则
            int subtype = paramMap.getIntMisValid("subtype");//子类型：1-优惠券；2-兑换券
            String pic = "";
            if (subtype == EnWzMapActivity.SUBTYPE_DHQ.getIndex()) {
                pic = paramMap.getStr("pic");//兑换物图片
            }
            String benefit = paramMap.getStrMisValid("benefit");//优惠方案
            String beginTimeX = paramMap.getStrMisValid("couponBeginTime");//礼券开始时间
            Date couponBeginTime = DateUtil.toDateTime(beginTimeX + " 00:00:00");
            String endTimeX = paramMap.getStrMisValid("couponEndTime");//礼券结束时间
            Date couponEndTime = DateUtil.toDateTime(endTimeX + " 23:59:59");
            int expense = totalNumber * Integer.parseInt(Consts.activityCouponFee);
            if(expense != amount){
                return Res.fail("金额错误!");
            }
            if (user.getCashBalance() < expense) {
                return Res.fail("余额不足,请充值!");
            }
            if (activityEndTime.after(advMap.getEndTime())) {
                return Res.fail("活动结束时间必须在周边店铺结束时间之前！");
            }
            if (activityEndTime.after(couponEndTime)) {
                return Res.fail("活动结束时间必须在礼券结束时间之前！");
            }
            coupon.setStock(totalNumber);
            coupon.setBeginTime(couponBeginTime);
            coupon.setEndTime(couponEndTime);
            coupon.setRule(rule);
            coupon.setSubtype(subtype);
            coupon.setBenefit(benefit);
            coupon.setPic(pic);
            //活动信息
            mapActivity.setBenefit(benefit);
            mapActivity.setSubtype(subtype);
            mapActivity.setPic(pic);
            boolean r = financeService.editMapActivity(expense,mapActivity, coupon,user);
            if (r) {
                return Res.success();
            } else {
                return Res.fail();
            }
        }else if(coupon.getType() == EnWzPrize.TYPE_Shopping_Coupon.getIndex()){
            List<WzCouponOrder> couponOrders = couponOrderMapper.selectList(new EntityWrapper<WzCouponOrder>().eq("user_code",userCode).eq("coupon_id",mapActivity.getCouponId()));//购买的券
            WzCouponOrder couponOrder = couponOrders.get(0);
            if (amount != 0) {
                return Res.fail("不需要付费!");
            }
            Date couponEndTime = coupon.getEndTime();
            //剩余数量
            if ((couponOrder.getOrderNumber() - couponOrder.getUsedNumber()) < totalNumber) {
                return Res.fail("优惠券库存不足!");
            }
            if (activityEndTime.after(advMap.getEndTime())) {
                return Res.fail("活动结束时间必须在周边店铺结束时间之前！");
            }
            if (activityEndTime.after(couponEndTime)) {
                return Res.fail("活动结束时间必须在礼券结束时间之前！");
            }
            couponOrder.setUsedNumber(couponOrder.getUsedNumber() + totalNumber);
            int r = mapActivityMapper.updateById(mapActivity);
            if(r > 0){
                r = couponOrderMapper.updateById(couponOrder);
                if(r > 0){
                    if (Consts.dev.equals("false")) {
                        String[] tel = new String[]{"15206121566", "17751585858"};
                        for (String c : tel) {
                            SendSmsResponse response = AliyunSmsUtil.sendAdminSms(c, "有活动修改需要审核", Consts.ADMIN_TEMPLATE_CODE);
                        }
                    }
                    return Res.success();
                }
            }
        }
        return Res.fail();
    }

    //参与活动
    public Long addUserActivity(Long mapActivityId, Long userCode) {
        WzMapActivity activity = cacheService.mapActivity(mapActivityId);
        //参与校验
        if (activity.getUsetype() == EnWzMapActivity.TYPE_DAILY.getIndex()) {//每天可领一次
            //进行中判断
            int count = userActivityMapper.selectCount(new EntityWrapper<WzUserActivity>().eq("map_activity_id", mapActivityId).eq("user_code", userCode).eq("status", EnWzMapActivity.USER_ACTIVITY_Going.getIndex()));
            if (count > 0) {
                return 1L;//活动进行中
            }
            //今天参与并完成判断
            Date today = DateUtil.toDateTime(DateUtil.toString(new Date()) + " 00:00:00");
            count = userActivityMapper.selectCount(new EntityWrapper<WzUserActivity>().eq("map_activity_id", mapActivityId).eq("user_code", userCode).eq("status", EnWzMapActivity.USER_ACTIVITY_End.getIndex()).ge("create_time", today));
            if (count > 0) {
                return 2L;//今天以完成
            }
        } else if (activity.getUsetype() == EnWzMapActivity.TYPE_ONLYONCE.getIndex()) {//只可以参与一次
            int count = userActivityMapper.selectCount(new EntityWrapper<WzUserActivity>().eq("map_activity_id", mapActivityId).eq("user_code", userCode));
            if (count > 0) {
                return 3L;//只可以参与一次
            }
        }
        //活动人数已满
        if (activity.getJoinNumber() >= activity.getTotalNumber()) {
            return 4L;//活动人数已满
        }
        //新增数据
        WzUserActivity userActivity = new WzUserActivity();
        userActivity.setMapActivityId(mapActivityId);
        userActivity.setCreateTime(new Date());
        if (activity.getUsetype() == EnWzMapActivity.TYPE_ONLYONCE.getIndex()) {
            int a = 2;
            int b = activity.getNeedShareNumber() / 25;
            a = a + b;
            Date date = DateUtil.getDateAfter(new Date(), a);
            if(date.before(activity.getEndTime())){
                userActivity.setEndTime(date);
            }else{
                userActivity.setEndTime(activity.getEndTime());
            }
        } else if (activity.getUsetype() == EnWzMapActivity.TYPE_DAILY.getIndex()) {
            int a = activity.getNeedShareNumber() / 25;
            if(a <= 0){
                a = 1;
            }
            Date date = DateUtil.getDateAfter(new Date(), a);
            if(date.before(activity.getEndTime())){
                userActivity.setEndTime(date);
            }else{
                userActivity.setEndTime(activity.getEndTime());
            }
        }
        userActivity.setUserCode(userCode);
        activity.setJoinNumber(activity.getJoinNumber() + 1);
        activity.setSurplusNumber(activity.getSurplusNumber()-1);
//        if(activity.getSurplusNumber()==0){
//            activity.setStatus(EnWzMapActivity.STATUS_End.getIndex());
//        }
        return saveUserActivity(activity,userActivity);
    }

    @Transactional
    public Long saveUserActivity(WzMapActivity mapActivity,WzUserActivity userActivity){
        int r = mapActivityMapper.updateById(mapActivity);
        if (r > 0) {
            r = userActivityMapper.insert(userActivity);
            if(r > 0){
                return userActivity.getUserMapActivityId();//成功
            }else{
                return 5L;//数据保存失败
            }
        }else{
            return 5L;//数据保存失败
        }
    }

    public Res joinActivity(ParamMap paramMap) throws Exception{
        Long mapActivityId = paramMap.getLongMisValid("mapActivityId");
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        WzMapActivity mapActivity = cacheService.mapActivity(mapActivityId);
        if(mapActivity.getJointype() == EnWzMapActivity.JOINTYPE_Direct_Join.getIndex()){
            //参与活动
            Long userMapActivityId = addUserActivity(mapActivityId, userCode);
            if(userMapActivityId > 6){
                return Res.success("参与成功！");
            } else{
                return Res.fail("此活动已参与！");
            }
        }else{
            return Res.fail("此活动是扫码参与活动！");
        }
    }

    public Res addMapActivity(ParamMap paramMap) {
        Long couponId = paramMap.getLongMisValid("couponId");//券id
        int needShareNumber = paramMap.getIntMisValid("needShareNumber");//需要分享人数
        Long userCode = jwtTokenUtil.getUserCodeFromToken();
        WzUser user = cacheService.user(userCode);//发布用户
        WzMapActivity mapActivity = new WzMapActivity();
//        WzAdvMap advMap = advMapMapper.selectById(advMapId);
//        mapActivity.setAdCode(advMap.getAdCode());
//        mapActivity.setMapActivityId(IdWorker.getId());
//        mapActivity.setLogo(advMap.getLogo());
//        mapActivity.setName(advMap.getName());
//        mapActivity.setAdvMapId(advMapId);
//        mapActivity.setUserCode(userCode);
//        mapActivity.setTotalNumber(totalNumber);
//        mapActivity.setSurplusNumber(totalNumber);
//        mapActivity.setNeedShareNumber(needShareNumber);
//        mapActivity.setJointype(jointype);
//        mapActivity.setUsetype(usetype);
//        mapActivity.setCreateTime(new Date());
//        mapActivity.setAdvMapCategoryFirst(advMap.getAdvMapCategoryFirst());
//        mapActivity.setStatus(EnWzMapActivity.STATUS_CHECKING.getIndex());
//        mapActivity.setBeginTime(activityBeginTime);
//        mapActivity.setEndTime(activityEndTime);
        return Res.success();
    }
}
